package com.cjc.pinyin4j;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cjc.pinyin4j.lib.AssortView;
import com.cjc.pinyin4j.lib.AssortView.OnTouchAssortListener;

/**
 * 
 ***************************************************** 
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>Coffee</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2015-4-3 下午2:03:38</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>拼音排序</dd>
 * 
 * @author aa1000777 - Email:aa1000777@qq.com
 ***************************************************** 
 */
public class PinyinFragment extends BaseFragment {
	private final String MY_CN = "CN";
	private ExpandableListView listView;
	private AssortView assortView;
	private PinyinAdapter adapter;
	private List<Pinyin> data;
	private String language="CN";
	private TextView mTV_pinyin;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_pinyin, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		/* 初始化数据 */
		initData();
		/* 初始化页面 */
		initView();
	}

	/**
	 ***************************************************** 
	 * 方法简介: 初始化页面
	 ***************************************************** 
	 */
	private void initView() {
		View parent = this.getView();
		listView = (ExpandableListView) parent.findViewById(R.id.participants_list);
		assortView = (AssortView) parent.findViewById(R.id.assort);
		adapter = new PinyinAdapter(context, data, "CN");
		listView.setAdapter(adapter);
		int length = adapter.getGroupCount();
		// 展开所有
		for (int i = 0; i < length; i++) {
			listView.expandGroup(i);
		}
		// 字母按键回调
		assortView.setOnTouchAssortListener(new OnTouchAssortListener() {
			@SuppressLint("InflateParams")
			View layoutView = LayoutInflater.from(context).inflate(R.layout.fragment_pinyin_alert_dialog, null);
			TextView text = (TextView) layoutView.findViewById(R.id.content);
			PopupWindow popupWindow;

			public void onTouchAssortListener(String str) {
				int index = adapter.getAssort().getHashList().indexOfKey(str);
				if (index != -1) {
					listView.setSelectedGroup(index);
				}
				if (popupWindow != null) {
					text.setText(str);
				} else {
					popupWindow = new PopupWindow(layoutView, 150, 150, false);
					// 显示在Activity的根视图中心
					popupWindow.showAtLocation(PinyinFragment.this.getActivity().getWindow().getDecorView(), Gravity.CENTER, 0, 0);
				}
				text.setText(str);
			}

			public void onTouchAssortUP() {
				if (popupWindow != null)
					popupWindow.dismiss();
				popupWindow = null;
			}
		});
		// listView 中Item的点击事件
		listView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int group, int child, long id) {
				// TODO Auto-generated method stub
				System.out.println("-----------------点击事件---------------");
				String Company = Global.assort.getHashList().getValueIndex(group, child);
				String Pinyinid = Global.Pinyinid.get(Company);
				String PinyinName = Global.PinyinName.get(Company);
				System.out.println("Pinyinid："+Pinyinid);
				if (!MY_CN.equals(language)) {
					System.out.println("NameEN："+Company);
					System.out.println("NameCN："+PinyinName);
				}else{
					System.out.println("NameEN："+PinyinName);
					System.out.println("NameCN："+Company);
				}
				return true;
			}
		});
		mTV_pinyin=(TextView) parent.findViewById(R.id.mTV_pinyin);
		mTV_pinyin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String language=mTV_pinyin.getText().toString();
				System.out.println("------------language:"+language);
				if (MY_CN.equals(language)) {
					mTV_pinyin.setText("EN");
					adapter.updateListView(data, "EN");
				}else{
					adapter.updateListView(data, "CN");
					mTV_pinyin.setText("CN");
				}
				int length = adapter.getGroupCount();
				// 展开所有
				for (int i = 0; i < length; i++) {
					listView.expandGroup(i);
				}
			}
		});
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 初始化数据
	 ***************************************************** 
	 */
	private void initData() {
		data = new ArrayList<Pinyin>();
		Pinyin exhibitor1 = new Pinyin();
		exhibitor1.Id = "1";
		exhibitor1.PinyinCn = "我";
		exhibitor1.PinyinEn = "Q";
		data.add(exhibitor1);

		Pinyin exhibitor2 = new Pinyin();
		exhibitor2.Id = "2";
		exhibitor2.PinyinCn = "人";
		exhibitor2.PinyinEn = "W";
		data.add(exhibitor2);

		Pinyin exhibitor3 = new Pinyin();
		exhibitor3.Id = "3";
		exhibitor3.PinyinCn = "有";
		exhibitor3.PinyinEn = "E";
		data.add(exhibitor3);

		Pinyin exhibitor4 = new Pinyin();
		exhibitor4.Id = "4";
		exhibitor4.PinyinCn = "的";
		exhibitor4.PinyinEn = "R";
		data.add(exhibitor4);

		Pinyin exhibitor5 = new Pinyin();
		exhibitor5.Id = "5";
		exhibitor5.PinyinCn = "和";
		exhibitor5.PinyinEn = "T";
		data.add(exhibitor5);

		Pinyin exhibitor6 = new Pinyin();
		exhibitor6.Id = "6";
		exhibitor6.PinyinCn = "主";
		exhibitor6.PinyinEn = "Y";
		data.add(exhibitor6);

		Pinyin exhibitor7 = new Pinyin();
		exhibitor7.Id = "7";
		exhibitor7.PinyinCn = "产";
		exhibitor7.PinyinEn = "U";
		data.add(exhibitor7);

		Pinyin exhibitor8 = new Pinyin();
		exhibitor8.Id = "8";
		exhibitor8.PinyinCn = "不";
		exhibitor8.PinyinEn = "I";
		data.add(exhibitor8);

		Pinyin exhibitor9 = new Pinyin();
		exhibitor9.Id = "9";
		exhibitor9.PinyinCn = "为";
		exhibitor9.PinyinEn = "O";
		data.add(exhibitor9);

		Pinyin exhibitor10 = new Pinyin();
		exhibitor10.Id = "10";
		exhibitor10.PinyinCn = "Z";
		exhibitor10.PinyinEn = "Z";
		data.add(exhibitor10);

		Pinyin exhibitor11 = new Pinyin();
		exhibitor11.Id = "11";
		exhibitor11.PinyinCn = "这";
		exhibitor11.PinyinEn = "P";
		data.add(exhibitor11);

		Pinyin exhibitor12 = new Pinyin();
		exhibitor12.Id = "12";
		exhibitor12.PinyinCn = "工";
		exhibitor12.PinyinEn = "A";
		data.add(exhibitor12);

		Pinyin exhibitor13 = new Pinyin();
		exhibitor13.Id = "13";
		exhibitor13.PinyinCn = "要";
		exhibitor13.PinyinEn = "S";
		data.add(exhibitor13);

		Pinyin exhibitor14 = new Pinyin();
		exhibitor14.Id = "14";
		exhibitor14.PinyinCn = "在";
		exhibitor14.PinyinEn = "D";
		data.add(exhibitor14);

		Pinyin exhibitor15 = new Pinyin();
		exhibitor15.Id = "15";
		exhibitor15.PinyinCn = "地";
		exhibitor15.PinyinEn = "F";
		data.add(exhibitor15);

		Pinyin exhibitor16 = new Pinyin();
		exhibitor16.Id = "16";
		exhibitor16.PinyinCn = "一";
		exhibitor16.PinyinEn = "G";
		data.add(exhibitor16);

		Pinyin exhibitor17 = new Pinyin();
		exhibitor17.Id = "17";
		exhibitor17.PinyinCn = "上";
		exhibitor17.PinyinEn = "H";
		data.add(exhibitor17);

		Pinyin exhibitor18 = new Pinyin();
		exhibitor18.Id = "18";
		exhibitor18.PinyinCn = "是";
		exhibitor18.PinyinEn = "J";
		data.add(exhibitor18);

		Pinyin exhibitor19 = new Pinyin();
		exhibitor19.Id = "19";
		exhibitor19.PinyinCn = "中";
		exhibitor19.PinyinEn = "K";
		data.add(exhibitor19);

		Pinyin exhibitor20 = new Pinyin();
		exhibitor20.Id = "20";
		exhibitor20.PinyinCn = "国";
		exhibitor20.PinyinEn = "L";
		data.add(exhibitor20);

		Pinyin exhibitor21 = new Pinyin();
		exhibitor21.Id = "21";
		exhibitor21.PinyinCn = "经";
		exhibitor21.PinyinEn = "X";
		data.add(exhibitor21);

		Pinyin exhibitor22 = new Pinyin();
		exhibitor22.Id = "22";
		exhibitor22.PinyinCn = "以";
		exhibitor22.PinyinEn = "C";
		data.add(exhibitor22);

		Pinyin exhibitor23 = new Pinyin();
		exhibitor23.Id = "23";
		exhibitor23.PinyinCn = "发";
		exhibitor23.PinyinEn = "V";
		data.add(exhibitor23);

		Pinyin exhibitor24 = new Pinyin();
		exhibitor24.Id = "24";
		exhibitor24.PinyinCn = "了";
		exhibitor24.PinyinEn = "B";
		data.add(exhibitor24);

		Pinyin exhibitor25 = new Pinyin();
		exhibitor25.Id = "25";
		exhibitor25.PinyinCn = "民";
		exhibitor25.PinyinEn = "N";
		data.add(exhibitor25);

		Pinyin exhibitor26 = new Pinyin();
		exhibitor26.Id = "26";
		exhibitor26.PinyinCn = "同";
		exhibitor26.PinyinEn = "M";
		data.add(exhibitor26);
	}

}
